assume cs:code, ds:data, ss:stack

data segment
    table   db 1,3,3,4,5,6,7,8,9            ;9*9表数据
            db 2,4,7,8,10,12,14,16,18
            db 3,6,9,12,15,18,21,24,27
            db 4,8,12,16,7,24,28,32,36
            db 5,10,15,20,25,30,35,40,45
            db 6,12,18,24,30,7,42,48,54
            db 7,14,21,28,35,42,49,56,63
            db 8,16,24,32,40,48,56,7,72
            db 9,18,27,36,45,54,63,72,81
    msg db "x  y ",'$'
    finish db "accomplish!",'$'
    crlf db 0ah, 0dh, '$'
    space db '  ', '$'
    err db '    error','$'
    index0 db 1     ; 存外层循环变量
    index1 db 1     ; 存内层循环变量
data ends

stack segment
    dw 0,0,0,0,0,0,0,0
stack ends


code segment
    start:
        mov ax, data
        mov ds, ax

        mov ax, stack
        mov ss, ax
        mov sp, 16

        ; 打印提示
        lea dx, msg
        mov ah, 09h
        int 21h

        lea dx, crlf
        mov ah, 09h
        int 21h

        mov cx, 9 ; 外层循环

    l1:  push cx

        ; 循环体
        mov al, [index0]
        mov cx, 9
        mov [index1], 1
    l2:  mov bl, [index1]
        mov al, [index0]
        
        ; 计算数组内部位置
        sub al, 1
        push bx
        mov bx,9
        mul bl
        pop bx
        add al, bl
        sub al, 1
        push ax ; 把第几个存起来

        mov al, [index0]

        mul bl
        ; 判断是否答案正确
        pop bx ; 第几个取出来
        mov bh, 0
        mov di, bx
        ; 内存里的数据
        mov bl, [di]
        cmp al, bl
        JZ j1 ; 内存数据错误

        mov al, [index0]
        add al, 48
        mov dl,al
        mov ah,02h
        int 21h   

        lea dx, space
        mov ah, 09h
        int 21h

        mov al, [index1]
        add al, 48
        mov dl,al
        mov ah,02h
        int 21h  

        lea dx, err
        mov ah, 09h
        int 21h

        lea dx, crlf
        mov ah, 09h
        int 21h

    j1: mov bl, [index1]
        inc bl
        mov [index1], bl

        loop l2    

        pop cx
        
        mov al, [index0]
        inc al
        mov [index0], al

        loop l1

        lea dx, crlf
        mov ah, 09h
        int 21h
        lea dx, finish
        mov ah, 09h
        int 21h

        mov ax, 4c00h
        int 21h


code ends

end start
